package com.kidedu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.ibatis.session.RowBounds;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;

import com.kidedu.dao.mapper.KGradeMapper;
import com.kidedu.dao.mapper.KStudentMapper;
import com.kidedu.dao.mapper.LinkedTableQueryMapper;
import com.kidedu.entity.KGrade;
import com.kidedu.entity.KGradeExample;
import com.kidedu.entity.KOrder;
import com.kidedu.entity.KStudent;
import com.kidedu.entity.KStudentExample;
import com.kidedu.entity.dto.KArtDTO;
import com.kidedu.entity.dto.KGASDTO;
import com.kidedu.entity.dto.KGradeDTO;
import com.kidedu.service.IKGradeService;
import com.kidedu.util.BaseService;
import com.kidedu.util.Pagination;

public class KGradeService extends BaseService implements IKGradeService{
	
	@Autowired
	private KGradeMapper kGradeMapper;
	
	@Autowired
	private KStudentMapper kStudentMapper;
	
	@Autowired
	private LinkedTableQueryMapper linkedTableQueryMapper;
	

	private int pageSize = 10;
	
	/*public int addKClass(KClass kClass) {
		return kClassMapper.insert(kClass);
	}*/
	
	
	public int deletekGradeList(String gId) {
		return kGradeMapper.deleteByPrimaryKey(gId);
	}
	
	
	/*public int updateKClass(KClass kClass) {
		return kClassMapper.updateByPrimaryKey(kClass);
	}*/
	
	
	public Pagination<KGradeDTO> selectkGradeList(int pageNo) {
		KGradeExample example = new KGradeExample();
		int totalCount = kGradeMapper.countByExample(example);
		List<KGradeDTO> kGradeDTOList =  linkedTableQueryMapper.selectkGradeList(new RowBounds(super.calculateStartPage(pageNo, pageSize), pageSize));
		return new Pagination<KGradeDTO>(pageNo, pageSize, totalCount, kGradeDTOList);
	}
	
	public JSONObject BatchImport(Sheet sheet) {
		JSONObject json=new JSONObject();
		int total=sheet.getLastRowNum();
		 Row row=null;
	    KGrade kGrade;
		 for(int i=0;i<=total;i++)
			{
				int j=-1;
				row=sheet.getRow(i);
				kGrade=new KGrade();
				row=sheet.getRow(i);
			
				 try{
					 row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
					 kGrade.setGid(row.getCell(0).getRichStringCellValue().toString());		
	
					 row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
					 kGrade.setGname(row.getCell(1).getRichStringCellValue().toString());		
	
					 row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);					
					 String classId=row.getCell(2).getRichStringCellValue().toString();
							 	
			    	 kGrade.setGclassid(classId);      		    
			     	// row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);			   			     
			     	 kGrade.setGbeginttime(row.getCell(3).getDateCellValue());		     	
					// row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
				     kGrade.setGendtime(row.getCell(4).getDateCellValue());			
					 row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
					 String Number=row.getCell(5).getRichStringCellValue().toString();
					  Byte convertToInteger=Byte.valueOf(Number);		
					 kGrade.setGnumber(convertToInteger);
					 System.out.println(5);
					 row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
					 String TeacherId=row.getCell(5).getRichStringCellValue().toString();
					
				    kGrade.setGteacherid(TeacherId);
				    System.out.println(6);
				     row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);				 	
				     kGrade.setGexpress(Float.valueOf(row.getCell(6).getRichStringCellValue().toString()));
				     System.out.println(7);
				     kGrade.setGstate(false);				 
				     j=kGradeMapper.insertSelective(kGrade);
				   if(j==-1)
				   {	
					   json.put("error", "");
				   }
				   }
				 catch (Exception e)
				 {
					 json.put("error2", "");
				 }
			
			}
		 json.put("message", "");
		return json;

	}


	@Override
	public Pagination<KGrade> selectkGradeList(int pageNo, String keywords) {
		KGradeExample example = new KGradeExample();
		example.createCriteria().andGstateEqualTo(true);
		if(keywords != null){
			keywords = "%" + keywords + "%";
			example.createCriteria().andGnameLike(keywords);
		}
		int totalCount = kGradeMapper.countByExample(example);
		List<KGrade> kGradeDTOList =  kGradeMapper.selectByExample(example,new RowBounds(super.calculateStartPage(pageNo, pageSize), pageSize));
		return new Pagination<KGrade>(pageNo, pageSize, totalCount, kGradeDTOList);
	}


	@Override
	public Pagination<KGrade> selectkGradeListPass(int pageNo) {
				KGradeExample example = new KGradeExample();
				example.createCriteria().andGstateEqualTo(true);
				int totalCount = kGradeMapper.countByExample(example);
				List<KGrade> kGradeDTOList = kGradeMapper.selectByExample(example,new RowBounds(super.calculateStartPage(pageNo, pageSize), pageSize));
				return new Pagination<KGrade>(pageNo, pageSize, totalCount, kGradeDTOList);
	}


	@Override
	public Pagination<KGrade> selectkGradeListByW(int pageNo, int week) {
				KGradeExample example = new KGradeExample();
				
				example.createCriteria().andGweekEqualTo(Byte.valueOf(""+week));
				int totalCount = kGradeMapper.countByExample(example);
				List<KGrade> kGradeDTOList = kGradeMapper.selectByExample(example,new RowBounds(super.calculateStartPage(pageNo, pageSize), pageSize));
				return new Pagination<KGrade>(pageNo, pageSize, totalCount, kGradeDTOList);
	}
	
	//家长查看开班情况
	//查看预设班级的相关信息、相关老师、是否开课，和孩子是否选择课程。
		public Pagination<KGradeDTO> selectkGradePreList(int pageNo, List<KOrder> kOrderList){
			KGradeExample example = new KGradeExample();
			int totalCount = kGradeMapper.countByExample(example);
			List<KGradeDTO> kGradeDTOList =  linkedTableQueryMapper.selectkGradeList(new RowBounds(super.calculateStartPage(pageNo, pageSize), pageSize));
			Date today = new Date();
			for(int i = 0; i < kGradeDTOList.size(); i++){
				for(int j = 0; j < kOrderList.size(); j++){
					if(kGradeDTOList.get(i).getgId() == kOrderList.get(j).getOsid()){
						kGradeDTOList.get(i).setIsSelected("是");
						break;
					}else{
						kGradeDTOList.get(i).setIsSelected("否");
					}
				}
				//System.out.println(kGradeDTOList.get(i).getgBeginttime());
				if(kGradeDTOList.get(i).getgBeginttime().getTime() <= today.getTime()){
					kGradeDTOList.get(i).setIsOpened("是");
				}else{
					kGradeDTOList.get(i).setIsOpened("否");
				}
			}
			return new Pagination<KGradeDTO>(pageNo, pageSize, totalCount, kGradeDTOList);
		}


		@Override
		public List<KGrade> selectMysGrade(List<String> gradeId) {
			KGradeExample example = new KGradeExample();
			example.createCriteria().andGidIn(gradeId);
			List<KGrade> gradeList=kGradeMapper.selectByExample(example);
			return gradeList;
		}
		
		
		/*获取某一教师所授班级及学生*/
		public KGASDTO getGASbyTea(String t_id){
			List<String> grade = new ArrayList<String>();
			
			KGradeExample kGradeExample = new KGradeExample();
			kGradeExample.createCriteria().andGteacheridEqualTo(t_id);
			List<KGrade> kGrades = kGradeMapper.selectByExample(kGradeExample);
			for(KGrade kGrade : kGrades){
				grade.add(kGrade.getGname());
			}
			
			KStudentExample kStudentExample = new KStudentExample();
			kStudentExample.createCriteria().andSclassIn(grade);
			List<KStudent> kStudents = kStudentMapper.selectByExample(kStudentExample);
			
			KGASDTO kgasdto = new KGASDTO(kGrades, kStudents);
			return kgasdto;
		}


		/*教师所授学生，分页*/
		public Pagination<KStudent> getStubyTeaPage(String t_id,int pageNo){
			List<String> grade = new ArrayList<String>();
			
			KGradeExample kGradeExample = new KGradeExample();
			kGradeExample.createCriteria().andGteacheridEqualTo(t_id);
			List<KGrade> kGrades = kGradeMapper.selectByExample(kGradeExample);
			for(KGrade kGrade : kGrades){
				grade.add(kGrade.getGname());
			}
			
			KStudentExample kStudentExample = new KStudentExample();
			kStudentExample.createCriteria().andSclassIn(grade);
			int totalCount = kStudentMapper.countByExample(kStudentExample);
			List<KStudent> kStudents = kStudentMapper.selectByExample(kStudentExample,new RowBounds(super.calculateStartPage(pageNo, pageSize), pageSize));
			return new Pagination<KStudent>(pageNo, pageSize, totalCount, kStudents);
		}
		
		@Override
		public Pagination<KGrade> selectkGradeListByC(int pageNo, String ClassId) {
			// TODO Auto-generated method stub
			KGradeExample example = new KGradeExample();
			example.createCriteria().andGclassidEqualTo(ClassId);
			int totalCount = kGradeMapper.countByExample(example);
			List<KGrade> kGradeDTOList = kGradeMapper.selectByExample(example,new RowBounds(super.calculateStartPage(pageNo, pageSize), pageSize));	
			return new Pagination<KGrade>(pageNo, pageSize, totalCount, kGradeDTOList);
		
		}


		@Override
		public boolean updateGrade(KGrade grade) {
			// TODO Auto-generated method stub
			
			kGradeMapper.updateByPrimaryKeySelective(grade);
			return true;
		}


		@Override
		public boolean addGrade(KGrade grade) {
			// TODO Auto-generated method stub
			KGradeExample example = new KGradeExample();
			example.setOrderByClause("Gid desc");
			List<KGrade> kGradeList=kGradeMapper.selectByExample(example);
			int pre=0;
			if(kGradeList.size()>0)
			{
				pre=Integer.parseInt(kGradeList.get(0).getGid());
			}
			grade.setGid(String.valueOf(pre + 1));
			grade.setGstate(false);
			kGradeMapper.insertSelective(grade);
			return true;
		}
}